'''※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
File Name: ini.py
Author: GID5564
Description: 
Version: 1.0
Created Time: 18/10/23-15:24:02
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※'''
  
#!/usr/bin/python
# -*- coding: UTF-8 -*-


from configparser import ConfigParser  
import os
import sys
import json

class IniConfig():
    def __init__(self,filename):
        
        self.path=filename
        self.conf = ConfigParser() 
        if os.path.exists(self.path):
            self.conf.read(self.path,encoding="utf-8")
        else:
            print(f"{self.path}文件不存在!")
            sys.exit()
            
    
    #获取所有的selections
    def get_all_selections(self):
        return self.conf.sections()

    #获取指定selection下的所有options
    def get_all_options(self,section):
        return self.conf.options(section)

    #获取指定selection下的指定option的值
    def get_key_value(self,section,option):
        return self.conf.get(section,option)
        
    #判断是否存在section
    def has_section(self,section):
        return self.conf.has_section(section)
        
    #判断是否存在option
    def has_option(self,section,option):
        return self.conf.has_option(section,option)


    #新增section
    def add_section(self,section):
        if section not in self.conf.sections():
            self.conf.add_section(section)
            with open(self.path, "w+") as f:
                self.conf.write(f)
            return True
        else:
            print(f"{section}已存在，请不要重复添加！")
            return False

    #新增更新section:option:value
    def add_value(self,section,option,value):
        if section not in self.conf.sections():
            self.conf.add_section(section)
        self.conf.set(section,option,value)
        with open(self.path, "w+") as f:
            self.conf.write(f)
        return True
    
    #删除section
    def remove_section(self,section):
        if section in self.conf.sections():
            self.conf.remove_section(section)
            with open(self.path, "w+") as f:
                self.conf.write(f)
            return True
        else:
            print(f"未找到{section}")
            return False
       
    
    #删除option
    def remove_option(self,section,option):
        if section in self.conf.sections():
            if option in self.conf.options(section):
                self.conf.remove_option(section,option)
                with open(self.path, "w+") as f:
                    self.conf.write(f)
                return True
            else:
                print(f"在{section}下未找到{option}")
                return False
        else:
            print(f"未找到{section}")
            return False
        

class JsonConfig():
    def __init__(self,filename):
        self.filename=filename
        self.data=self.__read_json__()
    #读取json
    def __read_json__(self):
        try:
            with open(self.filename, 'r', encoding='utf-8') as file:
                data = json.load(file)
            return data
        except Exception as e:
            print(f'读取JSON文件时出错: {e}')
            return None

    #写入json
    def __write_json__(self,data):
        try:
            with open(self.filename, 'w', encoding='utf-8') as file:
                json.dump(data, file, ensure_ascii=False, indent=4)
            print(f'数据已写入到 {self.filename}')
        except Exception as e:
            print(f'写入JSON文件时出错: {e}')
    #添加json
    def add_item(self, key, value):
        self.data[key] = value
        self.__write_json__(self.data)
   
    #更新json
    def update_item(self, key, new_value):
        if key in self.data:
            self.data[key] = new_value
            self.__write_json__(self.data)
    #删除json
    def delete_item(self, key):
        if key in self.data:
            del self.data[key]
            self.__write_json__(self.data)
    #查询json
    def get_item(self, key):
        return self.data.get(key, None)
    
    def has_item(self,key):
        if key in self.data:
            return True
        else:
            return False
        

"""
if __name__ == '__main__':
    path="/root/.aidboot/config/.config"
    ini=IniConfig(path)
    print(ini.get_key_value("git","repo"))
"""
























